package com.wz.controller;

import com.wz.config.Normal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthAccounts;
import org.web3j.protocol.http.HttpService;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.List;

/**
 * @program: project
 * @description: 账号管理
 * @author: Wangzheng
 * @create: 2020-12-16 12:34
 **/
@RestController
@RequestMapping("/AccountManage")
public class AccountManage {
	
	@Autowired
	private static Keys keys;

	@Autowired
	private static ECKeyPair keyPair;

	@Autowired
	private static Credentials credentials;
	
	@Autowired
    private static 	EthAccounts ethAccounts ;

	
	/**
	 * 创建账号
	 * http://localhost:8080/AccountManage/createNewAccount
	 */
	@RequestMapping("/createNewAccount")
	public void createNewAccount() {
		try {
			//创建一个秘钥对
			keyPair = keys.createEcKeyPair();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}
		//获取私钥:
		String privateKey = keyPair.getPrivateKey().toString(16);
		//获取公钥:
		String publicKey = keyPair.getPublicKey().toString(16);
		//生成账户:
		String account = keys.getAddress(keyPair);

		/*
		 * e12915eb85c3aadb41a0ef398c6c87536effc406f7f3ed4463bbe85cd290b838
		 * */
		System.out.println(privateKey);

		/*
		 * 3850b25c4ee87cce49d54774902006326629f798b733ccb717765f2a7e0c948f84fb82d9e238fc33e90273ff99f8afea6d772f04af1609ba2fe58f2f244f7788
		 * */
		System.out.println(publicKey);

		/*
		 * ae12c09b981ff677cdb2e2278c0f3cabf4a03c7b
		 * */
		System.out.println(account);
	}

	/*
	 * 导入私钥,通过私钥推导出账户信息
	 *
	 * */
	@RequestMapping("/importPrivateKey")
	public void importPrivateKey() {
		BigInteger privateKey = new BigInteger("e12915eb85c3aadb41a0ef398c6c87536effc406f7f3ed4463bbe85cd290b838", 16);
		//获取keyPair对象
		ECKeyPair keyPair = ECKeyPair.create(privateKey);
		//通过keyPair对象获取账户地址:
		String account = Keys.getAddress(keyPair);
		System.out.println(account);
	}

	/*
	 * 创建钱包文件
	 *  http://localhost:8080/AccountManage/createWalletFile
	 * */
	@RequestMapping("/createWalletFile")
	public void createWalletFile() {
		//指定密码:
		String pass = "123";
		//获取文件绝对路径
		File destDir = new File("D:\\develop\\demo\\src\\main\\resources\\keystore");
		String fn = null;

		try {
			//获取ecKeyPair
			ECKeyPair ecKeyPair = null;
			ecKeyPair = keys.createEcKeyPair();
			//获取钱包文件
			fn = WalletUtils.generateWalletFile(pass, ecKeyPair, destDir, true);
		} catch (CipherException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}
		System.out.println(fn);

	}

	/*
	 * 获取账户凭证:
	 *  http://localhost:8080/AccountManage/getUserDocument
	 * */
	@RequestMapping("/getUserDocument")
	public void getUserDocument(){
		//用户密码不能指定错误,否则无法解析!
		String pass = "123";
		try {
			//返回一个Credentials 凭证对象，此对象中包含了密钥对和账户地址信息：
			credentials = WalletUtils.loadCredentials(pass, "D:\\develop\\demo\\src\\main\\resources\\keystore\\UTC--2020-12-16T08-31-16.805000000Z--0015d85b2e10f41360c052a67c332e97a4aa9fcd.json");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (CipherException e) {
			e.printStackTrace();
		}
		//获取到秘钥对
		ECKeyPair keyPair = credentials.getEcKeyPair();
		//获取用户账户地址:
		String account = credentials.getAddress();
		//获取私钥:
		String privateKey = keyPair.getPrivateKey().toString(16);
		//获取公钥:
		String publicKey = keyPair.getPublicKey().toString(16);
		System.out.println(account);
		System.out.println(privateKey);
		System.out.println(publicKey);
	}
	
	/*
	* 节点管理的账户:获取节点管理的账户列表：
	*  http://localhost:8080/AccountManage/getNodeManagerUserList
	* */

	@RequestMapping("/getNodeManagerUserList")
	public void getNodeManagerUserList() { 
		Web3j web3j = Web3j.build(new HttpService(Normal.SERVER));
		try {
			Request<?, EthAccounts> request = web3j.ethAccounts();
			ethAccounts = request.send();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<String> accounts = ethAccounts.getAccounts();
		for (String account : accounts) {
			System.out.println(account);
		}
		/*
		*   0x0615ac2f83b745ac49bca28a38bd71abb479fa42
			0x4a623a6c970e82c0c4c5791c99307dec4ff173c1
			0x8ecba9d13bc34e8da8597f51e8edeb347eed10eb
			0x30af9a96bab21f9ff1300b07a2b8e57dac20ad8d
			0x4cabd67ba0acb54fd1026553139ba1ed5f156222
			0xf06740297a4eb5adf6561bd5cc1d826d4bc32362
			0x5abd395141b63bef98c6481f595dd2d9c592ca75
			0xa6f71fd31df22e3b0bad5f146fc9e901c659db81
			0x4139700991ffb12aece5761fbff8f0d65ac3ab24
			0x578232ae1db0e838c7d9f6509f81773be06c1dfe
			0x9acd10dd4bced5c197b93db445b96c17c1438f43
			0x340393bf726f79445ae88ad61ea0a29f9e79d65b
			0x6953ffcada3caf2d69964759208e3d08f88fb67c
            0x14d22e7a3cea21dd65bb65da54b83c9dfb7ac3c0
		* */
	}

		
}